<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Decodes a JSON string</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ref.simdjson.html">« Simdjson 函数</a></li>
      <li style="float: right;"><a href="function.simdjson-is-valid.html">simdjson_is_valid »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.simdjson.html">Simdjson 函数</a></li>
    <li>Decodes a JSON string</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.simdjson-decode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">simdjson_decode</h1>
  <p class="verinfo">(PECL simdjson &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">simdjson_decode</span> &mdash; <span class="dc-title">Decodes a JSON string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.simdjson-decode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>simdjson_decode</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$json</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$associative</code><span class="initializer"> = <strong><code>false</code></strong></span></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Takes a JSON encoded string and converts it into a PHP value.
   This uses a faster Simultaneous Instruction, Multiple Data implementation
   than <span class="function"><a href="function.json-decode.html" class="function">json_decode()</a></span> when it is supported by the computer architecture.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.simdjson-decode-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">json</code></dt>

    <dd>

     <p class="para">
      The <code class="parameter">json</code> <span class="type">string</span> being decoded.
     </p>
     <p class="para">
      This function only works with UTF-8 encoded strings.
     </p>
     <p class="para">
      This function parses valid inputs which
      <span class="function"><a href="function.json-decode.html" class="function">json_decode()</a></span> can decode,
      provided that they are less than 4 GiB long.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">associative</code></dt>

    <dd>

     <p class="para">
      When <strong><code>true</code></strong>, JSON objects will be returned as
      associative <span class="type">array</span>s; when <strong><code>false</code></strong>, JSON objects will be returned as <span class="type">object</span>s.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">depth</code></dt>

    <dd>

     <p class="para">
      Maximum nesting depth of the structure being decoded.
      The value must be greater than <code class="literal">0</code>,
      and less than or equal to <code class="literal">2147483647</code>.

      Callers should use reasonably small values,
      because larger depths require more buffer space and will
      increase the recursion depth, unlike the current <span class="function"><a href="function.json-decode.html" class="function">json_decode()</a></span> implementation.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.simdjson-decode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the value encoded in <code class="parameter">json</code> in appropriate
   PHP type. Values <code class="literal">true</code>, <code class="literal">false</code> and
   <code class="literal">null</code> are returned as <strong><code>true</code></strong>, <strong><code>false</code></strong> and <strong><code>null</code></strong>
   respectively.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.simdjson-decode-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   If <code class="parameter">json</code> is invalid, a <span class="classname"><a href="class.simdjsonexception.html" class="classname">SimdJsonException</a></span> is thrown as of PECL simdjson 2.1.0,
   while previously, a <span class="classname"><a href="class.runtimeexception.html" class="classname">RuntimeException</a></span> was thrown.
  </p>
  <p class="para">
   If <code class="parameter">depth</code> is outside the allowed range,
   a <span class="classname"><a href="class.simdjsonvalueerror.html" class="classname">SimdJsonValueError</a></span> is thrown as of PECL simdjson 3.0.0,
   while previously, an error of level <strong><code>E_WARNING</code></strong> was raised.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.simdjson-decode-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3422">
    <p><strong>示例 #1 <span class="function"><strong>simdjson_decode()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"a":1,"b":2,"c":3}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (3) {
  [&quot;a&quot;]=&gt;
  int(1)
  [&quot;b&quot;]=&gt;
  int(2)
  [&quot;c&quot;]=&gt;
  int(3)
}
array(3) {
  [&quot;a&quot;]=&gt;
  int(1)
  [&quot;b&quot;]=&gt;
  int(2)
  [&quot;c&quot;]=&gt;
  int(3)
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3423">
    <p><strong>示例 #2 Accessing invalid object properties</strong></p>
    <div class="example-contents"><p>
     Accessing elements within an object that contain characters not
     permitted under PHP&#039;s naming convention (e.g. the hyphen) can be
     accomplished by encapsulating the element name within braces and the apostrophe.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"foo-bar":&nbsp;12345}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">);<br />print&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'foo-bar'</span><span style="color: #007700">};&nbsp;</span><span style="color: #FF8000">//&nbsp;12345<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-3424">
    <p><strong>示例 #3 common mistakes using <span class="function"><strong>simdjson_decode()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;following&nbsp;strings&nbsp;are&nbsp;valid&nbsp;JavaScript&nbsp;but&nbsp;not&nbsp;valid&nbsp;JSON<br /><br />//&nbsp;the&nbsp;name&nbsp;and&nbsp;value&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br />//&nbsp;single&nbsp;quotes&nbsp;are&nbsp;not&nbsp;valid<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"{&nbsp;'bar':&nbsp;'baz'&nbsp;}"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Throws&nbsp;SimdJsonException<br /><br />//&nbsp;the&nbsp;name&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz"&nbsp;}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Throws&nbsp;SimdJsonException<br /><br />//&nbsp;trailing&nbsp;commas&nbsp;are&nbsp;not&nbsp;allowed<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz",&nbsp;}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Throws&nbsp;SimdJsonException<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-3425">
    <p><strong>示例 #4 <code class="parameter">depth</code> errors</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Encode&nbsp;some&nbsp;data&nbsp;with&nbsp;a&nbsp;maximum&nbsp;depth&nbsp;of&nbsp;4<br />//&nbsp;(array&nbsp;-&gt;&nbsp;array&nbsp;-&gt;&nbsp;array&nbsp;-&gt;&nbsp;string)<br /></span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'English'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'One'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'January'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'French'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Une'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Janvier'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]<br />&nbsp;&nbsp;&nbsp;&nbsp;]<br />);<br /><br /></span><span style="color: #FF8000">//&nbsp;Show&nbsp;the&nbsp;errors&nbsp;for&nbsp;different&nbsp;depths.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">));<br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">SimdJsonException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Caught:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
  [1]=&gt;
  array(2) {
    [&quot;English&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;One&quot;
      [1]=&gt;
      string(7) &quot;January&quot;
    }
    [&quot;French&quot;]=&gt;
    array(2) {
      [0]=&gt;
      string(3) &quot;Une&quot;
      [1]=&gt;
      string(7) &quot;Janvier&quot;
    }
  }
}
Caught: The JSON document was too deep (too many nested objects and arrays)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3426">
    <p><strong>示例 #5 <span class="function"><strong>simdjson_decode()</strong></span> of large integers</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"number":&nbsp;12345678901234567890}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">simdjson_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (1) {
  [&quot;number&quot;]=&gt;
  float(1.2345678901235E+19)
}
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.simdjson-decode-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    The JSON spec is not JavaScript, but a subset of JavaScript.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    In the event of a failure to decode,
    a <span class="classname"><a href="class.simdjsonexception.html" class="classname">SimdJsonException</a></span> is thrown
    and <span class="methodname"><strong>SimdJsonException::getCode()</strong></span> and
    <span class="methodname"><strong>SimdJsonException::getMessage()</strong></span> can be used
    to determine the exact nature of the error.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.simdjson-decode-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.json-encode.html" class="function" rel="rdfs-seeAlso">json_encode()</a> - 对变量进行 JSON 编码</span></li>
    <li class="member"><span class="function"><a href="function.json-decode.html" class="function" rel="rdfs-seeAlso">json_decode()</a> - 对 JSON 格式的字符串进行解码</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>